#!/usr/bin/env python
# -*- coding: utf-8 -*-

###########################################################
#                                                         #
# shooter - shooter game                                  #
#                                                         #
# 2011 - Andreas Grätz                                    #
#                                                         #
# andreaszdw@googlemail.com                               #
#                                                         #
# gameengine.py                                           #
#                                                         #
###########################################################

from PySFML import sf
import state
import stack

class GameEngine:
	def __init__(self, title="shooter", width=1024, height=768, bpp=0, fullscreen=False):
		# Create the window		
		self.window = sf.RenderWindow(sf.VideoMode(width, height), title) 
		# Center the window
		self.window.SetPosition(sf.VideoMode.GetDesktopMode().Width/2-width/2, sf.VideoMode.GetDesktopMode().Height/2-height/2)
		self.stack = stack.Stack()
		self.nextState = False
		
	def cleanup(self):
		while(not self.stack.empty()):
			self.stack.top().cleanup()
			self.stack.pop()
					
	def changeState(self, state):
		if(not self.stack.empty()):
			self.stack.top().cleanup()
			self.stack.pop()
			
		self.stack.push(state)
		self.stack.top().init()

	def pushState(self, state):
		if(not self.stack.empty()):
			self.stack.top().pause()
		
		self.stack.push(state)
		self.stack.top().init(self.window)
	
	def popState(self):
		if(not self.stack.empty()):
			self.stack.top().cleanup()
			self.stack.pop()
			self.stack.top().resume()
		
		if(self.stack.empty()):
			self.stack.top().resume()
	
	def top(self):
		return self.stack.top()
	
	def testrun(self):
		self.stack.top().isrunning()
	
	def isrunning(self):
		return self.stack.top().isrunning()
		
	def handleevents(self):
		self.stack.top().handleevents(self.window)
	
	def update(self):
		self.stack.top().update(self.window)
	
	def draw(self):
		self.stack.top().draw(self.window)
